// i.r(t), i.d(t, { default: () => SweepPucks })
import * as THREE from "three"
import { NavigateToSweepCommand } from "../command/navigate.command"
import { TogglePuckEditingCommand } from "../command/puck.command"
import configConst from "../constant/config.const"
import CameraData from "../data/CameraData"
import InteractionmodeData from "../data/InteractionmodeData"
import SettingsData from "../data/SettingsData"
import SweepViewData from "../data/SweepViewData"
import ViewmodeData from "../data/ViewmodeData"
import EngineContext from "../EngineContext"
import viewmodeEnum from "../enum/viewmode.enum"
import { ClickEvent } from "../event/GestureEvent"
import { PointerButtonEvent, PointerMoveEvent } from "../event/PointerEvent"
import TextureLoader from "../loader/TextureLoader"
import ModelColliderTarget from "../mesh/ModelColliderTarget"
import PuckCollider from "../mesh/PuckCollider"
import SkySphereMesh from "../mesh/SkySphereMesh"
import { PuckClickedMessage } from "../message/puck.message"
import { EndSwitchViewmodeMessage } from "../message/viewmode.message"
import u from "../packages/76870"
import PuckRenderer from "../renderer/PuckRenderer"
import SymbolList from "../SymbolList"
import Comparator from "../utils/Comparator"
import InputIniModule from "./InputIniModule"
import Module from "./Module"
import WebGLRendererModule from "./WebGLRendererModule"

const sweepImg = TextureLoader("./images/sweep-enable.png")
export const sweepStatus = { enabled: sweepImg, enabledHover: sweepImg, disabledHover: null, disabled: null }
export default class SweepPucksModule extends Module {
  IDLE_OPACITY: number
  EDITING_OPACITY: number
  IDLE_COLOR: THREE.Color
  SELECTION_COLOR: THREE.Color
  defaultCheckRenderModes: any
  unselectingSweep: any
  editingEnabled: boolean
  selectionBindings: any[]
  unselectionBindings: any[]
  selectionHandled: boolean
  unselectionHandled: boolean
  handlePuckClickedMessage: any
  sweepViewData: any
  cameraData: any
  engine: any
  interactionmodeData: any
  handleSweepSelectionChange: any
  selectedSweep: any
  updateViewmode: any
  viewmode: any
  viewmodeData: any
  unselectPuck: any
  onUnselectPuck: (e: any) => boolean
  onPointerOnPuck: (e: any, t: any) => void
  renderer: PuckRenderer
  selectionSub: any
  constructor() {
    super(...arguments)
    this.name = "sweep-pucks"
    this.IDLE_OPACITY = 0.3
    this.EDITING_OPACITY = 0.9
    this.IDLE_COLOR = new THREE.Color("white")
    this.SELECTION_COLOR = new THREE.Color(16724312)
    this.defaultCheckRenderModes = () => !0
    this.unselectingSweep = null
    this.editingEnabled = !1
    this.selectionBindings = []
    this.unselectionBindings = []
    this.selectionHandled = !0
    this.unselectionHandled = !0
    this.handlePuckClickedMessage = e => {
      if (!e) throw new Error("SweepPucks -> on PuckClickedMessage: Tried to move to invalid sweep id.")
      if (!this.canSelectPuck()) {
        this.sweepViewData.data.canTransition() &&
          this.cameraData.canTransition() &&
          this.engine.commandBinder.issueCommand(
            new NavigateToSweepCommand({
              transition: configConst.tourAniType[this.interactionmodeData.mode],
              sweep: e
            })
          )
      }
    }
    this.handleSweepSelectionChange = () => {
      this.updateHighlightOnSelectedPuck(!1)
      this.selectedSweep = this.sweepViewData.selectedSweep
      this.updateHighlightOnSelectedPuck(!0)
      this.updateHandlers()
    }
    this.updateViewmode = () => {
      this.viewmode = this.viewmodeData.currentMode
      this.updateHighlightOnSelectedPuck(this.editingEnabled)
      this.updateHandlers()
    }
    this.updateHandlers = () => {
      this.selectionHandled = this.toggleHandlers(this.canSelectPuck(), this.selectionBindings, this.selectionHandled)
      this.unselectionHandled = this.toggleHandlers(this.canUnselectPuck(), this.unselectionBindings, this.unselectionHandled)
    }
    this.unselectPuck = () => {
      this.unselectingSweep = null
      this.sweepViewData.setSelectedSweep(null)
    }
    this.onUnselectPuck = e => {
      const t = this.sweepViewData.selectedSweep
      t && (e.down ? (this.unselectingSweep = t) : this.unselectingSweep && this.unselectPuck())
      return !0
    }
    this.onPointerOnPuck = (e, t) => {
      if (e.button !== u.MP.PRIMARY) return
      const i = this.renderer.getSweepId(t.id)
      i &&
        (e.down
          ? i === this.selectedSweep
            ? (this.unselectingSweep = i)
            : ((this.unselectingSweep = null), this.sweepViewData.setSelectedSweep(i))
          : (i === this.unselectingSweep && this.unselectPuck(), (this.unselectingSweep = null)))
    }
  }
  updateHandlers: any
  async init(e, t: EngineContext) {
    this.engine = t
    void 0 !== e.checkRenderModes && (this.defaultCheckRenderModes = e.checkRenderModes)
    const WebGLScene = (await t.getModuleBySymbol<WebGLRendererModule>(SymbolList.WEBGL_RENDERER)).getScene()
    const input = await t.getModuleBySymbol<InputIniModule>(SymbolList.INPUT)
    const [settings, cameraData, sweepViewData, viewmodeData, interactionmodeData] = await Promise.all([
      t.market.waitForData(SettingsData),
      t.market.waitForData(CameraData),
      t.market.waitForData(SweepViewData),
      t.market.waitForData(ViewmodeData),
      t.market.waitForData(InteractionmodeData)
    ])
    this.viewmodeData = viewmodeData
    this.viewmode = viewmodeData.currentMode
    this.sweepViewData = sweepViewData
    this.cameraData = cameraData
    this.interactionmodeData = interactionmodeData
    this.renderer = new PuckRenderer(
      WebGLScene.scene,
      input,
      settings,
      sweepViewData,
      sweepStatus,
      !0,
      this.defaultCheckRenderModes,
      this.IDLE_COLOR,
      this.SELECTION_COLOR,
      this.IDLE_OPACITY,
      this.EDITING_OPACITY,
      void 0,
      void 0,
      t.claimRenderLayer(this.name)
    )
    t.addComponent(this, this.renderer)
    this.bindings.push(
      t.commandBinder.addBinding(TogglePuckEditingCommand, async e => this.togglePuckEditing(e.enabled)),
      t.subscribe(PuckClickedMessage, e => this.handlePuckClickedMessage(e.sweepId))
    )
    this.selectionBindings.push(
      input.registerMeshHandler(PointerButtonEvent, Comparator.isType(PuckCollider), this.onPointerOnPuck),
      input.registerHandler(PointerMoveEvent, () => {
        this.unselectingSweep = null
      })
    )
    this.selectionHandled = this.toggleHandlers(!1, this.selectionBindings, this.selectionHandled)
    this.selectionSub = this.sweepViewData.onSelectedSweepChanged(this.handleSweepSelectionChange)
    this.selectionSub.cancel()
    this.unselectionBindings.push(
      input.registerPriorityHandler(ClickEvent, ModelColliderTarget, () => !0),
      input.registerPriorityHandler(PointerButtonEvent, ModelColliderTarget, this.onUnselectPuck),
      input.registerPriorityHandler(PointerButtonEvent, SkySphereMesh, this.onUnselectPuck)
    )
    this.unselectionHandled = this.toggleHandlers(!1, this.unselectionBindings, this.unselectionHandled)
  }
  dispose(e) {
    for (const e of this.bindings) e.cancel()
    this.bindings = []
    this.togglePuckEditing(!1)
    super.dispose(e)
  }
  updatePuckImagery(e = {}) {
    const t = Object.assign(Object.assign({}, sweepStatus), e)
    t.disabled && !t.disabledHover && (t.disabledHover = t.disabled), this.renderer.updatePuckImagery(t)
  }
  updateCheckRenderModes(e) {
    this.renderer.updateCheckRenderModes(e || this.defaultCheckRenderModes)
  }
  updateHighlightOnSelectedPuck(e) {
    const t = this.selectedSweep
    if (t) {
      if (!this.sweepViewData.isSweepAligned(t)) return
      const i = this.viewmode === viewmodeEnum.Dollhouse || this.viewmode === viewmodeEnum.Floorplan
      this.renderer.renderPuckHighlight(t, this.editingEnabled && i && e)
    }
  }
  togglePuckEditing(e) {
    this.editingEnabled = e
    this.selectedSweep = this.sweepViewData.selectedSweep
    this.updateViewmode()
    this.renderer.toggleEditingEnabled(e)
    e
      ? (this.engine.subscribe(EndSwitchViewmodeMessage, this.updateViewmode), this.selectionSub.renew())
      : (this.engine.unsubscribe(EndSwitchViewmodeMessage, this.updateViewmode), this.selectionSub.cancel())
  }
  canUnselectPuck() {
    const e = this.viewmode === viewmodeEnum.Dollhouse || this.viewmode === viewmodeEnum.Floorplan
    return this.editingEnabled && !!this.selectedSweep && e
  }
  canSelectPuck() {
    const e = this.viewmode === viewmodeEnum.Dollhouse || this.viewmode === viewmodeEnum.Floorplan
    return this.editingEnabled && e
  }
  toggleHandlers(e, t, i) {
    if (e !== i) for (const i of t) e ? i.renew() : i.cancel()
    return e
  }
}
